<template>
  <div class="page-search">
    <!-- 使用组件的数据双向绑定， 组件内默认使用modelValue接收 -->
    <sz-form v-bind="formConfig" v-model="formData">
      <template #header>
        <h2>高级检索</h2>
      </template>
      <template #footer>
        <div class="handleBtn">
          <el-button icon="el-icon-refresh" @click="handleResetClick"
            >重置</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleQueryClick"
            >搜索</el-button
          >
        </div>
      </template>
    </sz-form>
    <!-- <sz-form :formItems="formItems" :label-width="labelWidth"></sz-form> -->
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue"
import SzForm from "@/base-ui/form"

export default defineComponent({
  components: {
    SzForm
  },
  props: {
    formConfig: {
      type: Object,
      required: true
    }
  },
  emits: ["resetBtnClick", "searchBtnClick"],
  setup(props, { emit }) {
    // formData中的属性应该根据props中的对象决定
    const formItems = props.formConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ""
    }
    const formData = ref(formOriginData)

    // 重置
    const handleResetClick = () => {
      // formData.value = formOriginData
      for (const key in formOriginData) {
        formData.value[`${key}`] = formOriginData[key]
      }

      emit("resetBtnClick")
    }

    // 点击搜索
    const handleQueryClick = () => {
      emit("searchBtnClick", formData.value)
    }
    return {
      formData,
      handleResetClick,
      handleQueryClick
    }
  }
})
</script>

<style scoped lang="scss">
.page-search {
  .sz-form {
    padding-top: 22px;
  }

  .handleBtn {
    text-align: right;
    padding: 0 50px 10px 0;
  }
}
</style>
